﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for clsTableAction
/// </summary>
public class clsTableAction : clsConnection
{
    private string _tableName;
    public clsTableAction(string tableName)
    {
        _tableName = tableName;
    }
    //Dua du lieu vao DataReader
    public SqlDataReader exportDataReader(string cols, string cri)
    {
        try
        {
            connectDatabase();
            string sql = "select " + cols + "  from " + _tableName + ((cri.Length == 0) ? "" : " where " + cri).ToString();
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = _sqlConn;
            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.CommandText = sql;
            return (sqlComm.ExecuteReader());
        }
        catch
        {
            return null;
        }
    }
    public DataTable exportDataTable(string cols, string cri)
    {
        try
        {
            connectDatabase();
            DataTable dtTable = new DataTable();
            string sql = "select " + cols + "  from " + _tableName + ((cri.Length == 0) ? "" : " where " + cri).ToString();
            SqlDataAdapter sqlDaAd = new SqlDataAdapter(sql, _sqlConn);
            sqlDaAd.Fill(dtTable);
            return (dtTable);
        }
        catch
        {
            return null;
        }
    }
    //Dua du lieu vao luoi
    public void exportDataToGridview(ref GridView gv, string cols, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(cols, cri);
        gv.DataSource = sqlDare;
        gv.DataBind();
    }
    //Dua du lieu vao luoi co phan trang
    public void exportDataToGridviewPaging(int pageIndex, int pageSize, ref GridView gv, string cols, string cri)
    {
        DataTable dtTable = this.exportDataTable(cols, cri);
        gv.DataSource = dtTable;
        gv.AllowPaging = true;
        gv.PageSize = pageSize;
        gv.PageIndex = pageIndex;
        gv.DataBind();

    }
    public Boolean exportDataToGridview(string cols, string cri, ref GridView gv, int rowsPag, int pagIndex)
    {
        try
        {
            DataTable tbl = exportDataTable(cols, cri);
            gv.DataSource = tbl;
            //Tien hanh phan trang
            gv.AllowPaging = true;
            gv.PageIndex = pagIndex;
            gv.PageSize = rowsPag;
            gv.DataBind();
            free();
            return true;
        }
        catch
        {
            return false;
        }
    }
    public void exportDataToDropDownList(ref DropDownList drop, string txt, string val, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(txt + ", " + val, cri);
        drop.DataSource = sqlDare;
        drop.DataTextField = txt;
        drop.DataValueField = val;
        drop.DataBind();
    }
    public void exportDataToListBox(ref ListBox lst, string txt, string val, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(txt + ", " + val, cri);
        lst.DataSource = sqlDare;
        lst.DataTextField = txt;
        lst.DataValueField = val;
        lst.DataBind();
    }
    public void exportDataToCheckBoxList(ref CheckBoxList lst, string txt, string val, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(txt + ", " + val, cri);
        lst.DataSource = sqlDare;
        lst.DataTextField = txt;
        lst.DataValueField = val;
        lst.DataBind();
    }
    public Boolean insert(string vals, out int kq)
    {
        kq = 0;
        string sql = "insert into " + _tableName + " values(" + vals + ")";
        try
        {
            Boolean bl = runSQL(sql, out kq);
            free();
            return bl;
        }
        catch
        {
            return false;
        }
    }

    public Boolean update(string cols, string vals, string whe, out int kq)
    {
        kq = 0;
        string[] col = cols.Split(',');
        string[] val = vals.Split('|');
        string sets = "";
        for (int i = 0; i < col.Length; i++)
        {
            if (i > 0) sets += ", ";
            sets += col[i] + "=" + val[i];
        }
        string sql = "update " + _tableName + " set " + sets + " where " + whe;
        try
        {
            Boolean bl = runSQL(sql, out kq);
            free();
            return bl;
        }
        catch
        {
            return false;
        }
    }

    public Boolean delete(string whe, out int kq)
    {
        kq = 0;
        string sql = "delete " + _tableName + " where " + whe;
        try
        {
            Boolean bl = runSQL(sql, out kq);
            free();
            return bl;
        }
        catch
        {
            return false;
        }
    }

    public Boolean maxCol(string col, out object kq)
    {
        kq = null;
        try
        {
            string sql = "select max(" + col + ") from " + _tableName;
            if (!runSQLValue(sql, out kq)) return false;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public Boolean getCol(string col, string whe, out object kq)
    {
        kq = null;
        try
        {
            if (runSQLValue("select " + col + " from " + _tableName + " where " + whe, out kq))
                return true;
            else
                return false;
        }
        catch
        {
            return false;
        }
    }
}
